package com.foreknow.controller;

import com.foreknow.model.UserEntity;
import com.foreknow.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class MainController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/admin/user",method = RequestMethod.GET)
    public String getUser(ModelMap modelMap){//查询所有用户的信息
        //调用持久层方法
        List<UserEntity> list=userRepository.findAll();
        //将查询到的list保存到ModelMep中
        modelMap.addAttribute("userList",list);
        return "admin/user";
    }

    @RequestMapping(value = "/admin/user/delete/{id}",method = RequestMethod.GET)
    //利用注解获取id内容
    public String addUser(@PathVariable("id") Integer userId){
        //删除对应id数据
        userRepository.delete(userId);
        //刷新数据
        userRepository.flush();
        //利用redirect:进行重定向
        return "redirect:/admin/user";
    }
}
